﻿// dllmain.cpp : 定义 DLL 应用程序的入口点。
#include "pch.h"
#include <string>
#include "../../../win_com/xx_mem.hpp"
#include "../../../win_com/xx_inline_hook.hpp"

#include "time.h"


char tra_cw[1024];

BOOL WINAPI MyCreateProcessW(
    _In_opt_ LPCWSTR lpApplicationName,
    _Inout_opt_ LPWSTR lpCommandLine,
    _In_opt_ LPSECURITY_ATTRIBUTES lpProcessAttributes,
    _In_opt_ LPSECURITY_ATTRIBUTES lpThreadAttributes,
    _In_ BOOL bInheritHandles,
    _In_ DWORD dwCreationFlags,
    _In_opt_ LPVOID lpEnvironment,
    _In_opt_ LPCWSTR lpCurrentDirectory,
    _In_ LPSTARTUPINFOW lpStartupInfo,
    _Out_ LPPROCESS_INFORMATION lpProcessInformation
) {
    time_t now = time(nullptr);
    printf("================\n");
    printf("%s", ctime(&now));
    wprintf(L"execute=%s\npara=%s\n\n" ,lpApplicationName,lpCommandLine );

    auto ori = xx_trampoline_to_func(&CreateProcessW, tra_cw);

    return (*ori)(lpApplicationName,
        lpCommandLine,
        lpProcessAttributes,
        lpThreadAttributes,
        bInheritHandles,
        dwCreationFlags,
        lpEnvironment,
        lpCurrentDirectory,
        lpStartupInfo,
        lpProcessInformation);
}


BOOL APIENTRY DllMain( HMODULE hModule,
                       DWORD  ul_reason_for_call,
                       LPVOID lpReserved
                     )
{
    static bool init = false;
    if (!init) {
        init = true;

        if (AllocConsole())
        {
            freopen("CONOUT$", "w", stdout);
            freopen("CONIN$", "r", stdin);
            SetConsoleTitleA("Console");
            SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
            printf("dll injectok!\n");
        }

        {
            // 制作跳板
            xx_mem_unprotect(tra_cw, 1024);
            xx_make_trampoline(&CreateProcessW, tra_cw, 0xf);

            // HOOK
            xx_mem_unprotect(&CreateProcessW, 1024);
            xx_setjmp(&CreateProcessW, &MyCreateProcessW);
        }
    }

    return TRUE;
}

